package com.springboot.project.common.controller;

import com.springboot.config.annotion.BusinessLog;
import com.springboot.config.exception.BussinessException;
import com.springboot.config.tool.*;
import com.springboot.project.common.entity.Dept;
import com.springboot.project.common.entity.Role;
import com.springboot.project.common.entity.User;
import com.springboot.project.common.service.impl.DeptServiceImpl;
import com.springboot.project.common.service.impl.RoleServiceImpl;
import com.springboot.project.common.service.impl.UserServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author lby
 * @since 2018-03-24
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    DeptServiceImpl deptService;

    @Autowired
    RoleServiceImpl roleService;

    private static String PREFIX = "system/user/";

    /**
     * 跳转到查看管理员列表的页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "user";
    }

    /**
     * 加载表格
     * @param params
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        int offset = Integer.parseInt(params.get("offset").toString());
        int limit = Integer.parseInt(params.get("limit").toString());
        params.put("page", offset /  limit + 1 );
        params.put("offset", offset);
        params.put("limit", limit);
        List<User> sysUserList = userService.selectUser(params);
        int total = userService.count(params);
        PageUtils pageUtil = new PageUtils(sysUserList, total);
        return pageUtil;
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping("/add")
    public String addView() {
        return PREFIX + "user_add";
    }

    /**
     * 跳转到编辑管理员页面
     */
    @RequestMapping("/edit/{id}")
    public String userEdit(@PathVariable Integer id, Model model) {

        User user = userService.selectById(id);
        model.addAttribute("user",user);
        //部门
        Dept dept = deptService.selectById(user.getDeptid());
        if(dept != null) {
            model.addAttribute("deptName", dept.getFullname());
        }
        //角色
        Role role = roleService.selectById(user.getRoleid());
        if(role != null) {
            model.addAttribute("roleName", role.getName());
        }
        return PREFIX + "user_edit";
    }

    /**
     * 删除用户
     */
    @RequestMapping("/remove/{id}")
    @BusinessLog(value = "删除用户")
    @RequiresPermissions("/user/del")
    @ResponseBody
    public Tip remove(@PathVariable Integer id) {
        User user = userService.selectById(id);
        if("admin".equals(user.getUsername()))
        {
            throw new BussinessException(500, "不能删除admin该用户！");
        }
        userService.deleteById(id);
        return new Tip();
    }

    /**
     * 重置用户密码
     */
    @RequestMapping("/resetPassword/{id}")
    @BusinessLog(value = "用户重置密码")
    @RequiresPermissions("/user/reset")
    @ResponseBody
    public Tip resetPassword(@PathVariable Integer id) {
        String password = ToolUtil.md5("123456","zdog");
        userService.resetPassword(id, password);
        return new Tip();
    }

    /**
     * 添加用户
     */
    @RequestMapping("/addUser")
    @BusinessLog(value = "添加用户")
    @RequiresPermissions("/user/add")
    @ResponseBody
    public Tip addUser(User user) {
        ToolUtil.SetUpUserDefault(user);
        userService.insert(user);
        return new Tip();
    }

    /**
     * 编辑用户
     */
    @RequestMapping("/editUser")
    @BusinessLog(value = "修改用户")
    @RequiresPermissions("/user/edit")
    @ResponseBody
    public Tip editUser(User user) {
        userService.updateById(user);
        return new Tip();
    }

    @RequestMapping("/exit")
    @ResponseBody
    public boolean exit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !userService.exit(params);
    }

    /**
     * 跳转到查看管理员列表的页面
     */
    @RequestMapping("/deptTreeView")
    public String deptTreeView() {
        return PREFIX + "deptTreeView";
    }

    /**
     * 跳转到角色分配的页面
     */
    @RequestMapping("/roleTreeView")
    public String roleTreeView() {
        return PREFIX + "roleTreeView";
    }

}
